package com.example.demo.controller;

import com.example.demo.entity.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;
    @GetMapping("/save")
    public void save() {
        studentRepository.saveString("uname","张安");
        Student s1 = new Student("001","战三","1223@qq.com",12);
        studentRepository.saveStudent(s1);
        Student s2 = new Student("002","李四","1223@qq.com",13);
        Student s3 = new Student("003","王五","12234@qq.com",21);
        List<Student> stuts = new ArrayList<>();
        stuts.add(s2);
        stuts.add(s3);
        studentRepository.saveMultiStudents("mutilStus",stuts);
    }
    @GetMapping("/getUname")
    @Cacheable(value = "myuname")
    public String getUname(String key) {
        System.out.println("测试缓存");
        return studentRepository.getString(key);
    }
    @GetMapping("/getStudent")
    public Student getStudent(String key) {return (Student) studentRepository.getObject(key);}
    @GetMapping("/getMultiStus")
    public List<Student> getMultiStus(String key) {return (List<Student>) studentRepository.getObject(key);}
}